package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.domain.MessagePushHistory;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDoctorService;
import com.naiterui.ehp.bs.doctor.service.IIndexService;
import com.naiterui.ehp.bs.doctor.vo.NoticeListTypeVO;
import com.naiterui.ehp.bs.doctor.vo.NoticePageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <P>功能描述 : 账户动态 ,通知</P>
 * <P>File name : NoticeController </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/4/8 下午4:41</P>
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.NOTICE_DYNAMICS})
@Controller
@RequestMapping("ad")
public class AdNoticeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdIndexController.class);

    @Autowired
    private IIndexService indexService;

    @Autowired
    private IDoctorService doctorService;

    /**
     * APP医生端获取个人账户动态列表（H5展示）
     * IndexController.messageCenter()
     *
     * @param doctorId
     * @param pageParam
     *
     * @return
     *
     * @Date 2016年1月20日
     * @since 2.0.0
     */
    @ApiIgnore
    @RequestMapping("user/dynamics")
    @ResponseBody
    public PageVO<MessagePushHistory> messageCenter(Long doctorId, PageParamVO pageParam) {

        LOGGER.info("index uer dynamics INFO ; doctorId {},pageParam {}", doctorId, pageParam);
        return this.indexService.getUserDynamics(doctorId, pageParam);

    }

    /**
     * APP端系统公告列表
     * IndexController.noticePage()
     *
     * @return
     *
     * @Date 2016年3月28日
     * @since 2.0.0
     */
    @RequestMapping("index/noticePage")
    @ApiOperation(value = "[2.17修改]获取系统公告列表", notes = "获取系统公告列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "limitTime", value = "左滑删除标识时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "topic", value = "公告主题ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "请求页数", required = false, dataType = "string", paramType = "query")})
    @ResponseBody
    public ResponseVO<PageVO<NoticePageVO>> noticePage(Long doctorId, Integer sort, String limitTime, Long topic,
                                                       PageParamVO pageParam) throws BusinessException {
        PageVO<NoticePageVO> resultPage = this.indexService.getNoticePage(doctorId, sort, limitTime, topic, pageParam);
        LOGGER.info("doctor index noticePage SUCCESS ; doctorId={},sort={},limitTime={},topic={},pageParam={}", doctorId, sort, limitTime, topic,
                    pageParam);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, resultPage);
    }

    /**
     * 系统公告详情
     * IndexController.noticePage()
     *
     * @param doctorId
     * @param noticeId
     *
     * @return
     *
     * @throws Exception
     * @Date 2016年1月22日
     * @since 2.0.0
     */
    @RequestMapping("index/noticeDetail")
    @ApiOperation(value = "[2.17修改]查看系统公告详情", notes = "查看系统公告详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "公告id", required = true, dataType = "long", paramType = "query")})
    @ResponseBody
    public ResponseVO<NoticePageVO> noticeDetail(Long doctorId, Long noticeId) throws Exception {
        LOGGER.info("查看系统公告详情doctorId:{},noticeId:{}", doctorId, noticeId);
        if (null == noticeId) {
            LOGGER.error("doctor index noticeDetil ERROR ; illegal param : doctorId={}，noticeId=null", doctorId);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        NoticePageVO noticePageVO = this.indexService.getNoticeDetail(doctorId, noticeId);
        LOGGER.info("doctor index noticeDetail SUCCESS ; doctor={},noticeId={}", doctorId, noticeId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, noticePageVO);

    }


    /**
     * 新用户首次登陆初始化公告信息
     * IndexController.initNotice()
     *
     * @param doctorId
     * @param initType 新增初始化类型(0：无需初始化;1：新用户初始化;2：老用户新版本初始化)
     *
     * @return
     *
     * @Date 2016年1月12日
     * @since 2.0.0
     */
    @ApiIgnore
    @RequestMapping("/index/initNotice")
    @ResponseBody
    public void initNotice(Long doctorId, Integer initType) throws Exception {
        if (null == doctorId || null == initType) {
            LOGGER.error("doctor index initNotice ERROR;illegal param:doctorId {},initType {}", doctorId, initType);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        // (0：无需初始化;1：新用户初始化;2：老用户新版本初始化)无需初始化直接返回结束
        if (initType == 0) {
            throw new BusinessException(ExceptionCodes.SUCCESS);
        }

        this.doctorService.getNewDoctorNotice(doctorId, initType);
        LOGGER.info("doctor index initNotice SUCCESS ; doctorId {},initType {}", doctorId, initType);

    }


    /**
     * 系统公告列表类型
     * NoticeController.getNoticeListType()
     *
     * @param doctorId
     *
     * @return
     *
     * @throws BusinessException
     * @Author lichaopi
     * @Date 2018年7月26日
     * @since 1.0.0
     */
    @ApiOperation(value = "系统公告列表类型", notes = "系统公告列表类型", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "Long", paramType = "query")})
    @ResponseBody
    @RequestMapping("notice/type/list")
    public List<NoticeListTypeVO> getNoticeListType(Long doctorId) throws Exception {
        LOGGER.info("获取系统公告列表类型,doctorId:{}", doctorId);
        if (doctorId == null) {
            LOGGER.info("获取系统公告列表类型，参数为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.indexService.getNoticeListType(doctorId);
    }

}
